﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;
using Process;

namespace QuanLyDiem
{
    public partial class frmMonHoc_Sua : Form
    {
        private static frmMonHoc_Sua frm = null;

        public event UpdateMonHoc UpdateSuccess = null;

        private BoMon bomon = new BoMon();
        private MonHoc monhoc = null;

        private frmMonHoc_Sua()
        {
            InitializeComponent();
            cmbBoMon.DataSource = bomon.ListName();
        }

        public static frmMonHoc_Sua Create()
        {
            if (frm == null)
            {
                frm = new frmMonHoc_Sua();
            }
            return frm;
        }

        public static frmMonHoc_Sua Create(MonHoc monhoc)
        {
            if (frm == null)
            {
                frm = new frmMonHoc_Sua();
            }
            frm.monhoc = monhoc;
            frm.UpdateData();
            return frm;
        }

        private void UpdateData()
        {
            txtMa.Text = monhoc.ThongTin.Ma;
            txtTen.Text = monhoc.ThongTin.Ten;
            txtSoTC.Text = monhoc.ThongTin.SoTinChi.ToString();
            txtDiemQT.Text = monhoc.ThongTin.HeSoDiemQT;
            cmbBoMon.Text = monhoc.ThongTin.BoMon;
            txtDiemT.Text = monhoc.ThongTin.HeSoDiemT;
            txtGhiChu.Text = monhoc.ThongTin.GhiChu;
        }

        private void frmMonHoc_Sua_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtDiemQT_Leave(object sender, EventArgs e)
        {
            if (txtDiemQT.Text != "0")
            {
                if (Convert.ToDouble(txtDiemQT.Text.Replace('.', ',')) > 1)
                {
                    MessageBox.Show("Hệ số điểm quá trình phải nhỏ hơn 1", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtDiemQT.Focus();
                    return;
                }
                string[] temp = txtDiemQT.Text.Split('.', ',');
                int DiemQT = int.Parse(temp[1]);
                string DiemT = "0." + (10 - DiemQT).ToString();
                txtDiemT.Text = DiemT;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            monhoc.ThongTin.Ten = txtTen.Text;
            monhoc.ThongTin.SoTinChi = int.Parse(txtSoTC.Text);
            monhoc.ThongTin.HeSoDiemQT = txtDiemQT.Text;
            monhoc.ThongTin.BoMon = cmbBoMon.Text;
            monhoc.ThongTin.GhiChu = txtGhiChu.Text;
            int check = monhoc.Update();
            if (check > 0)
            {
                if (UpdateSuccess != null)
                {
                    InfoUpdateMonHoc info = new InfoUpdateMonHoc();
                    info.MonHoc = this.monhoc;
                    UpdateSuccess(this, info);
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("Có vấn đề khi sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void txtSoTC_TextChanged(object sender, EventArgs e)
        {
            txtSoTC.Text = txtSoTC.Text.Trim();
            if (!string.IsNullOrEmpty(txtSoTC.Text))
            {
                if (!xString.IsNumber(txtSoTC.Text, StyleNumber.INTEGER))
                {
                    MessageBox.Show("Bạn chỉ được nhập số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtSoTC.Text = txtSoTC.Text.Trim().Substring(0, txtSoTC.Text.Trim().Length - 1);
                    txtSoTC.Select(txtSoTC.Text.Length, 0);
                }
            }
        }

        private void txtDiemQT_TextChanged(object sender, EventArgs e)
        {
            txtDiemQT.Text = txtDiemQT.Text.Trim();
            if (!xString.IsNumber(txtDiemQT.Text, StyleNumber.REAL))
            {
                MessageBox.Show("Bạn chỉ được nhập số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDiemQT.Text = txtDiemQT.Text.Trim().Substring(0, txtDiemQT.Text.Trim().Length - 1);
                txtDiemQT.Select(txtDiemQT.Text.Length, 0);
            }
        }
    }
}
